package com.lyx.tracksolution;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.lyx.tracksolution.databinding.ActivityViewOnclickListenerBinding;

import butterknife.ButterKnife;
import butterknife.OnClick;

public class ViewOnclickListenerActivity extends AppCompatActivity {

    private Button mBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityViewOnclickListenerBinding dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_view_onclick_listener);
        dataBinding.setHandlers(this);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        return super.onContextItemSelected(item);
    }

    @Override
    @SuppressWarnings("all")
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_more:
                ViewGroup rootView = findViewById(R.id.rootView);
                AppCompatButton button = new AppCompatButton(this);
                button.setText("动态创建的 Button");
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(ViewOnclickListenerActivity.this, "动态创建的 Button", Toast.LENGTH_SHORT).show();
                    }
                });
                rootView.addView(button);
                Toast.makeText(ViewOnclickListenerActivity.this, "MenuItem", Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initView() {
        mBtn = findViewById(R.id.btn);
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(ViewOnclickListenerActivity.this, "设置mOnClickListener对象", Toast.LENGTH_SHORT).show();
            }
        });
        findViewById(R.id.image_button).setOnClickListener(v -> {
            Toast.makeText(ViewOnclickListenerActivity.this, "含有Lambda语法的mOnClickListener", Toast.LENGTH_SHORT).show();
        });
    }

    public void tvCLick(View view) {
        Toast.makeText(ViewOnclickListenerActivity.this, "android:onClick方式", Toast.LENGTH_SHORT).show();
    }

    @OnClick(R.id.iv)
    void ivClick() {
        Toast.makeText(ViewOnclickListenerActivity.this, "ButterKnife使用", Toast.LENGTH_SHORT).show();
    }

    public void dataBindingOnClick(View view) {
        Toast.makeText(ViewOnclickListenerActivity.this, "dataBinding的使用", Toast.LENGTH_SHORT).show();
    }

}